package com.example.demo.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.demo.entity.VeVe;
import com.example.demo.service.VeVeService;

//@RestController
@Controller
@RequestMapping("ve")
public class VeVeController {

	@Resource
	private VeVeService veVeService;
	@RequestMapping("insert")
	@ResponseBody
	public VeVe insert(VeVe veve) {
		return veVeService.insert(veve);
	}
	
	@PreAuthorize("hasAnyAuthority('WORER','SHENHE')")
	@RequestMapping("find")
	@ResponseBody
	public List<VeVe> findAll() {
		if(1==1)throw new RuntimeException("出错了");
			

		return veVeService.findAll();
	}
	@PostMapping("head_img")
	@ResponseBody
	public String upload(MultipartFile file,String uname) {
		String path="D:\\中级项目\\zhongji-static\\image";
		String contentType = file.getContentType();
		System.out.println(contentType);
		String filename = file.getOriginalFilename();
		File file2 = Paths.get(path, filename).toFile();
		try {
			FileOutputStream out = new FileOutputStream(file2);
			BufferedOutputStream out2 = new BufferedOutputStream(out);
			out2.write(file.getBytes());
		} catch ( IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	
	@GetMapping("upload")
	public String upload() {
		return "upload";
	}
}
